package com.tm.mygsyplayer;

import androidx.appcompat.app.AppCompatActivity;

import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;

import com.shuyu.gsyvideoplayer.GSYVideoManager;
import com.shuyu.gsyvideoplayer.utils.OrientationUtils;
import com.tm.mygsyplayer.widget.MyGsyPlayer;

public class MyGsyPlayerActivity extends AppCompatActivity {

    MyGsyPlayer gsyPlayer;
    private OrientationUtils orientationUtils;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_gsy_player);

        gsyPlayer = findViewById(R.id.my_gsy_player);

        String url = "http://gslb.miaopai.com/stream/Kkfwus-oq0FvDYFSM2ZdUA7fpvF~Eb7AOgaggw__.mp4?vend=miaopai&ssig=985a58a4a43d9dcb51e498d922f0cb60&time_stamp=1568709306250&mpflag=32&unique_id=1568705667098034";
        String title = "测试视频";

        gsyPlayer.setUp(url, false, title);
        //增加封面
        ImageView imageView = new ImageView(this);
        imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
        imageView.setImageResource(R.mipmap.ic_video_cover);
        gsyPlayer.setThumbImageView(imageView);
//        //增加title
//        videoPlayer.getTitleTextView().setVisibility(View.VISIBLE);
//        //设置返回键
//        videoPlayer.getBackButton().setVisibility(View.VISIBLE);
        //设置旋转
        orientationUtils = new OrientationUtils(this, gsyPlayer);

        //设置全屏按键功能,这是使用的是选择屏幕，而不是全屏
//        gsyPlayer.getFullscreenButton().setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                orientationUtils.resolveByClick();
//            }
//        });
        //是否可以滑动调整
        gsyPlayer.setIsTouchWiget(true);
//        //设置返回按键功能
//        gsyPlayer.getBackButton().setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                onBackPressed();
//            }
//        });
        gsyPlayer.startPlayLogic();
    }

    @Override
    protected void onPause() {
        super.onPause();
        gsyPlayer.onVideoPause();
    }

    @Override
    protected void onResume() {
        super.onResume();
        gsyPlayer.onVideoResume();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        GSYVideoManager.releaseAllVideos();
        if (orientationUtils != null)
            orientationUtils.releaseListener();
    }

    @Override
    public void onBackPressed() {
        //先返回正常状态
        if (orientationUtils.getScreenType() == ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE) {
            gsyPlayer.getFullscreenButton().performClick();
            return;
        }
        //释放所有
        gsyPlayer.setVideoAllCallBack(null);
        super.onBackPressed();
    }


}
